﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Modelo_ETdA.Negocio.Analise.Formato;
using Modelo_ETdA.Negocio.Analise;

namespace Modelo_ETdA.Interface
{
    public partial class VerFormatoCliente : Form
    {
        private GestorAnalise gestor_analise;
        private DataTable cons1;
        private DataTable cons2;
        private DataTable cons3;
        private DataTable cons4;
        private Dictionary<int, List<QuestaoCliente>> dicQuestoes;
        private Dictionary<int, Consideracao> dicConsideracoes;
        private MenuAnaliseETdA mAnaletda;
        private FormatoAvaliacaoCliente formato;
        private int numConsAnal;
        private int contQuest;

        public VerFormatoCliente(GestorAnalise gestor, MenuAnaliseETdA mAnaliseetda, int codForm)
        {
            InitializeComponent();
            this.gestor_analise = gestor;
            this.mAnaletda = mAnaliseetda;
            this.dicQuestoes = new Dictionary<int, List<QuestaoCliente>>();
            this.dicConsideracoes = new Dictionary<int, Consideracao>();
            this.formato = (FormatoAvaliacaoCliente)gestor_analise.consultaFormatoAvaliacao(codForm, 1, gestor_analise.AmbComercialSessao.Cabeçalho.Cod);
            this.nomeFormato.Text = formato.Nome;
            this.perc.Text = formato.ValorCliente + "";
            this.numConsAnal = 1;
            this.contQuest = 0;

            foreach (Consideracao cons in gestor_analise.consultaListaConsideracao())
            {
                this.dicConsideracoes.Add(cons.Cod, cons);
            }
            foreach (QuestaoCliente qc in formato.ListQuestaoCliente)
            {
                if (!dicQuestoes.ContainsKey(qc.CodConsideracao))
                {
                    dicQuestoes[qc.CodConsideracao] = new List<QuestaoCliente>();
                }
                dicQuestoes[qc.CodConsideracao].Add(qc);
            }
            mostra4();
        }

        private void mostra4()
        {
            cons1 = new DataTable();
            cons2 = new DataTable();
            cons3 = new DataTable();
            cons4 = new DataTable();
            int contador = 0;
            for (int i = 1; i <= 4 && numConsAnal < dicConsideracoes.Count + 1; i++, numConsAnal++)
            {
                if (dicQuestoes.ContainsKey(numConsAnal))
                {
                    contador++;
                    contQuest++;
                    DataGridView auxDGV = new DataGridView();
                    DataTable auxDT = new DataTable();
                    Label auxLB = null;
                    switch (i)
                    {
                        case 1:
                            auxDGV = consideracao1;
                            auxDT = cons1;
                            auxLB = nomeCons1;
                            break;
                        case 2:
                            auxDGV = consideracao2;
                            auxDT = cons2;
                            auxLB = nomeCons2;
                            break;
                        case 3:
                            auxDGV = consideracao3;
                            auxDT = cons3;
                            auxLB = nomeCons3;
                            break;
                        case 4:
                            auxDGV = consideracao4;
                            auxDT = cons4;
                            auxLB = nomeCons4;
                            break;
                    }
                    auxLB.Text = dicConsideracoes[numConsAnal].Descricao;
                    inicializaTabelas(auxDGV, auxDT);
                }
                else
                {
                    i--;
                }
            }
            if (contador < 4)
            {
                switch (contador)
                {
                    case 1:
                        consideracao2.Visible = false;
                        consideracao3.Visible = false;
                        consideracao4.Visible = false;
                        nomeCons2.Visible = false;
                        nomeCons3.Visible = false;
                        nomeCons4.Visible = false;
                        labelCon2.Visible = false;
                        labelCon3.Visible = false;
                        labelCon4.Visible = false;
                        break;
                    case 2:
                        consideracao3.Visible = false;
                        consideracao4.Visible = false;
                        nomeCons3.Visible = false;
                        nomeCons4.Visible = false;
                        labelCon3.Visible = false;
                        labelCon4.Visible = false;
                        break;
                    case 3:
                        nomeCons4.Visible = false;
                        consideracao4.Visible = false;
                        labelCon4.Visible = false;
                        break;
                }
            }
            if ((dicQuestoes.Count - contQuest) == 0)
            {
                novaCon.Visible = false;
            }
        }

        public void inicializaTabelas(DataGridView grid1, DataTable tab)
        {
            tab.Columns.Add("Nome");
            tab.Columns.Add("Min. Escala");
            tab.Columns.Add("Max. Escala");
            tab.Columns.Add("Valor Mínimo");
            tab.Columns.Add("Ponderação %");
            tab.Columns["Nome"].MaxLength = 25;
            tab.Columns["Min. Escala"].MaxLength = 1;
            tab.Columns["Max. Escala"].MaxLength = 3;
            tab.Columns["Valor Mínimo"].MaxLength = 1;
            tab.Columns["Ponderação %"].MaxLength = 3;

            foreach (QuestaoCliente qc in dicQuestoes[numConsAnal])
            {
                DataRow row = tab.NewRow();
                row["Nome"] = qc.Alcunha;
                row["Min. Escala"] = qc.EscalaMin;
                row["Max. Escala"] = qc.EscalaMax;
                row["Valor Mínimo"] = qc.MinResposta;
                row["Ponderação %"] = Math.Round(qc.Ponderacao*100);
                tab.Rows.Add(row);
            }
            if (contQuest <= 4)
            {
                grid1.DataSource = tab;
                grid1.Columns[0].Width += 18;
                grid1.Columns[1].Width -= 20;
                grid1.Columns[3].Width -= 20;
                grid1.Columns[3].Width -= 20;
                grid1.Columns[4].Width -= 15;
            }
        }

        private void RegistaFormatoCliente_FormClosed(object sender, FormClosedEventArgs e)
        {
            mAnaletda.sair();
        }

        private void voltar_Click(object sender, EventArgs e)
        {
            mAnaletda.Visible = true;
            this.Visible = false;
        }

        private void novaCon_Click(object sender, EventArgs e)
        {
            try
            {
                mostra4();
            }
            catch (Exception exp)
            {
                MessageBox.Show("Erro na consulta do formato.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
